#!/usr/bin/python
# plot_MassDistrib.py

import numpy
from matplotlib import use as useBackend
useBackend('Agg')
import matplotlib.pyplot as ppt


def cheat111():
    '''Cheat to clear plot.'''
    ppt.subplot(311)
    ppt.plot([0,1],[1,2])
    ppt.subplot(131)
    ppt.plot([0,1],[1,2])
    ppt.subplot(111)


def plot(masstuple, simpars, phypars):
    '''Shows a slice of GM_MassDistrib's results.'''
    for i in simpars:
        cmd = "%s = simpars['%s']" % (i,i)
        exec cmd
    for i in phypars:
        cmd = "%s = phypars['%s']" % (i,i)
        exec cmd
    (darkstars, rho0mlist) = masstuple
    # Plot the development of the zero-point of density with mass
    masses = MvMass[:,1]
    logmass = numpy.log10(masses)
    cheat111()
    logrho = numpy.log10(rho0mlist)
    ppt.plot(logmass, logrho)
    ppt.plot(logmass, logrho, c='k', ls='none', marker=',')
#    ppt.plot(masses, rho0mlist)
#    ppt.plot(masses, rho0mlist, c='k', ls='none', marker=',')
    ppt.xlabel('Mass in M_sun')
    ppt.ylabel('Log10 Stars per mass interval (per cubic parsec?)')
    ppt.title('Stellar mass versus local density')
    plotname = outdir+'Mass_vs_density.pdf'
    ppt.savefig(plotname)
    # The mass intervals
    cheat111()
    logdm = numpy.log10(delMvMass[:,1])
    ppt.plot(masses, logdm)
    ppt.plot(masses, logdm, c='k', ls='none', marker=',')
    ppt.xlabel('Mass in M_sun')
    ppt.ylabel('Log10 mass interval')
    ppt.title('Stellar mass versus mass interval')
    plotname = outdir+'Mass_vs_massinterval.pdf'
    ppt.savefig(plotname)
    # The final result
    densities = numpy.ones(len(darkstars), dtype=float)
    for i in range(0, len(darkstars)):
        densities[i] = darkstars[i][0,0,0]
    cheat111()
    ppt.plot(masses, densities)
    ppt.plot(masses, densities, c='k', ls='none', marker=',')
    ppt.xlabel('Mass in M_sun')
    ppt.ylabel('Stars in mass interval (per cubic parsec?)')
    ppt.title('Stellar mass versus local density in interval')
    plotname = outdir+'Mass_vs_density_result.pdf'
    ppt.savefig(plotname)
    # And development of scale height with mass
    cheat111()
    ppt.plot(masses, MvMassScales)
    ppt.plot(masses, MvMassScales, c='k', ls='none', marker=',')
    ppt.xlabel('Mass in M_sun')
    ppt.ylabel('Scale height')
    ppt.title('Stellar mass versus scale height')
    plotname = outdir+'Mass_vs_scaleheight.pdf'
    ppt.savefig(plotname)
    # Plot two slices each for a massive and diminutive population
    diminstars = darkstars[0]
    burlystars = darkstars[-1]
    # Diminutive stars
    cheat111()
    ppt.imshow(diminstars[:,:,0])
    ppt.title("Density of low-mass stars for b ~ 0")
    ppt.colorbar()
    ppt.xlabel('0 -> l -> pi')
    ppt.ylabel('large -> d -> small')
    plotname = outdir+'LoMassStarsldPlane.pdf'
    ppt.savefig(plotname)

    cheat111()
    ppt.imshow(diminstars[:,0,:])
    ppt.title("Density of low-mass stars for l ~ 0")
    ppt.colorbar()
    ppt.xlabel('0 -> b -> pi/2')
    ppt.ylabel('large -> d -> small')
    plotname = outdir+'LoMassStarsbdPlaneWindward.pdf'
    ppt.savefig(plotname)

    cheat111()
    ppt.imshow(diminstars[:,-1,:])
    ppt.title("Density of low-mass stars for l ~ pi")
    ppt.colorbar()
    ppt.xlabel('0 -> b -> pi/2')
    ppt.ylabel('large -> d -> small')
    plotname = outdir+'LoMassStarsbdPlaneLeeward.pdf'
    ppt.savefig(plotname)

    # Burly stars
    cheat111()
    ppt.imshow(burlystars[:,:,0])
    ppt.title("Density of high-mass stars for b ~ 0")
    ppt.colorbar()
    ppt.xlabel('0 -> l -> pi')
    ppt.ylabel('large -> d -> small')
    plotname = outdir+'HiMassStarsldPlane.pdf'
    ppt.savefig(plotname)

    cheat111()
    ppt.imshow(burlystars[:,0,:])
    ppt.title("Density of high-mass stars for l ~ 0")
    ppt.colorbar()
    ppt.xlabel('0 -> b -> pi/2')
    ppt.ylabel('large -> d -> small')
    plotname = outdir+'HiMassStarsbdPlaneWindward.pdf'
    ppt.savefig(plotname)

    cheat111()
    ppt.imshow(burlystars[:,-1,:])
    ppt.title("Density of high-mass stars for l ~ pi")
    ppt.colorbar()
    ppt.xlabel('0 -> b -> pi/2')
    ppt.ylabel('large -> d -> small')
    plotname = outdir+'HiMassStarsbdPlaneLeeward.pdf'
    ppt.savefig(plotname)

